package com.ble.testpost;

import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.ble.testpost.http.AppHttpClient;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumFile;
import com.yanzhenjie.album.api.widget.Widget;

import java.io.File;
import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private CompositeDisposable compositeDisposable = new CompositeDisposable();

    private ArrayList<AlbumFile> mAlbumFiles;

    private ImageView imageView;

    private String filePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        Button selectBtn = findViewById(R.id.select_img_btn);
        Button uploadBtn = findViewById(R.id.upload_img_btn);
        imageView = findViewById(R.id.image_view);
        selectBtn.setOnClickListener(this);
        uploadBtn.setOnClickListener(this);
    }


    private void uploadImg(){
        if (TextUtils.isEmpty(filePath)){
            Toast.makeText(this, "图片不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        File file = new File(filePath);
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("data", file.getName(), requestBody);

        AppHttpClient.ApiService().uploadImage(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean<String> stringBaseBean) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        compositeDisposable.dispose();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.select_img_btn:
                selectImage();
                break;
            case R.id.upload_img_btn:
                uploadImg();
                break;
        }
    }

    /**
     * Select picture, from album.
     * 图片选择，采用的album第三方库，地址https://github.com/yanzhenjie/Album
     * 后期如果需要修改可以clone源码修改, 本项目直接采用com.yanzhenjie:album:version方式引用
     */
    private void selectImage() {
        Album.image(this)
                .multipleChoice()
                .requestCode(200)
                .camera(true)       //是否带拍照
                .columnCount(2)     //图片选择列数
                .selectCount(1)     //最大图片选择数
                .checkedList(mAlbumFiles)
                .widget(
                        Widget.newDarkBuilder(this)
                                .title("图片选择")
                                .build()
                )
                .onResult(new Action<ArrayList<AlbumFile>>() {
                    @Override
                    public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
                        mAlbumFiles = result;   //选择的图片的集合
                        //选择照片成功
                        //GlideUtil.getInstance().loadImage(UserDetailActivity.this, userIc, mAlbumFiles.get(0).getPath(), R.drawable.ic_user_default);
                        String path = mAlbumFiles.get(0).getPath();
                        String avatorPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath();
                        String finishPath = avatorPath + System.currentTimeMillis() + ".jpg";
                        ImageHandle.getimage(path, finishPath, avatorPath);

                       filePath = finishPath;
                       imageView.setImageBitmap(BitmapFactory.decodeFile(finishPath));
                    }
                })
                .onCancel(new Action<String>() {
                    @Override
                    public void onAction(int requestCode, @NonNull String result) {
                        //取消
                    }
                })
                .start();
    }
}
